﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    [CustomPropertyDrawer(typeof(EnumIntAttribute))]
    public sealed class EnumIntPropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType !=  SerializedPropertyType.Integer) return;

            int iValue = property.intValue;
            int selectIndex = -1;
            var attributes = base.fieldInfo.GetCustomAttributes(typeof(EnumIntAttribute), false);
            GUIContent[] contents = new GUIContent[attributes.Length];
            int[] tempValues = new int[attributes.Length];
            for(int i = 0; i < attributes.Length; i++)
            {
                int index = attributes.Length - 1 - i;
                var item = (EnumIntAttribute)attributes[index];
                contents[i] = new GUIContent(item.Label);
                tempValues[i] = item.Value;
                if(item.Value == iValue)
                {
                    selectIndex = i;
                }
            }

            EditorGUI.BeginChangeCheck();
            selectIndex = EditorGUI.Popup(position, label, selectIndex, contents);
            if (EditorGUI.EndChangeCheck())
            {
                property.intValue = tempValues[selectIndex];
            }
        }




    }
}

